﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTGroup_ZheJiang.WebUI.Code;
using System.Data;
using DTGroup.Networking.Framework.Utils.Web;
using DTGroup_ZheJiang.Business;
using DTGroup_ZheJiang.Models;
using DTGroup.Networking.Framework.Utils;
namespace DTGroup_ZheJiang.WebUI.List
{
    public partial class SenseStatisticsReport : BasePage
    {
        /// <summary>
        /// 每页显示记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 当前索引页
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount;
        public string sqlCondition = string.Empty;
        public DataTable dataSource { get; set; }
        public string tableName;
        public SenseStatisticsBO bo = new SenseStatisticsBO();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = 15;//每页条数
                TotalCount = 0;
                PageIndex = RequestUtil.Get<int>("page", Method.All, 1);//当前页

                if (PageIndex <= 0)
                    PageIndex = 1;

                ((DTGroup_ZheJiang.WebUI.MainMaster)Master).Crumb = "楼层级统计报表";
                string ReportTime = RequestUtil.GetString("ReportTime");

                if (!string.IsNullOrEmpty(ReportTime))
                {
                    string Options = RequestUtil.GetString("Options");
                    string ddlType = RequestUtil.GetString("ddlType");
                    string ddlCity = RequestUtil.GetString("ddlCity");
                    string ddlSense = RequestUtil.GetString("ddlSense");
                  
                    tableName = "ZJ2_SenseSta_" + getTabName(ddlType, ddlCity);
                    sqlCondition = " where 1=1 ";
                    if (ddlSense != null && ddlSense != "全部")
                    {
                        sqlCondition += " and Sense_Name='" + ddlSense + "'";
                    }
                  
                    sqlCondition += @" and P_Day=to_date('" + Convert.ToDateTime(ReportTime).ToString("yyyy-MM-dd") + "','yyyy-MM-dd') ";

                    if (Options == "Search")
                    {
                        dataSource = bo.GetSenseStatisticsByPage(tableName, PageIndex, PageSize, sqlCondition, out TotalCount);
                    }
                    else if (Options == "Export")
                    {
                        Export();
                    }
                    else
                    {
                        dataSource = new DataTable();
                    }
                }
                else
                {
                    dataSource = new DataTable();
                }
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        public void Export()
        {
            string root = System.Configuration.ConfigurationManager.AppSettings["ExportExcelPath"];
            string fileRootDir = MapPath(root + "/fileReport/");
            string fileName = string.Concat("导出", "_", DateTime.Now.ToString("yyyyMMddHHmmss"), "_", new Random().Next(1, 10000), ".csv");
            string filePath = IOUtil.JoinPath(fileRootDir, fileName);
            List<SenseStatistics> list = bo.GetSenseStatisticsByWhere(tableName, sqlCondition);

            DTGroup_ZheJiang.Utility.ExcelUtil.ListToCsv<SenseStatistics>(list, "日期,城市,场景,场景建筑名,经度,纬度,楼宇数,楼宇MR采样点数,楼宇弱覆盖MR采样点数,楼宇覆盖率,"
            + "楼宇平均RSRP,室外采样点数,室外弱覆盖采样点数,室外覆盖率,室外平均RSRP,场景id", filePath, (SenseStatistics item) =>
            {
                return string.Concat(item.P_Day, ",", item.City, ",", item.Sense_Name, ",", item.Sense_PointName + "," + item.Longitude, ",", item.Latitude, ",",
                    item.BuildingsCount, ",", item.BuildingsMRCount, ",", item.BuildingsWeakMRCount, ",", item.BuildingsCoverPercent, ",", item.BuildingsAvgRsrp
                    , ",", item.OutMRCount, ",", item.OutWeakMRCount, ",", item.OutCoverPercent, ",", item.OutAvgRSRP, ",", item.SenseId);
            }, System.Text.Encoding.Default);

            base.JsonWriter2(fileName, "/fileReport/" + fileName);
        }
    }
}